class Stack {
    constructor() {
        //栈
        this.items = {};//存放元素
        this.count = 0;
    }
    //入栈
    push(element) {
        this.items[this.count] = element;
        this.count++;
    }
    //出栈
    pop() {
        if (this.isEmpty()) {
            return undefined
        }
        let target = this.items[this.count - 1]
        delete this.items[this.count - 1]
        this.count--;
        return target
    }
    //返回栈顶元素
    peek() {
        return this.items[this.count - 1]
    }
    //栈是否为空
    isEmpty() {
        return this.count === 0
    }
    //清空栈
    clear() {
        this.items = {};
        this.count = 0;
    }
    //返回栈的元素个数
    size() {
        return this.count
    }
    //转字符串
    toString() {
        if (this.isEmpty()) {
            return ''
        }
        let str = this.items[0];
        for (let i = 1; i < this.count; i++) {
            str = `${str},${this.items[i]}`
        }
        return str
    }
}
let S = new Stack()

S.push('a')
S.push('b')
S.push('c')
S.pop()

console.log(S.toString())
console.log(S)

//十进制转二进制
function tenTwo(num) {
    let rem = '';
    let str = '';
    let s = new Stack()
    while (num != 0) {
        console.log(num)
        //存储余数
        rem = Math.floor(num % 2)
        //入栈
        s.push(rem)
        //存储整数
        num = Math.floor(num / 2)
    }
    let count = s.size();
    for(let i = 0 ; i < count ; i++){
        str += `${s.pop()}`
    }
    console.log(str)
}
tenTwo(10)